PHP - MYSQL Help

kk's Avatar

kk

27 Jul, 2009 12:03 PM via

Hi all. I am very new to PHP and MYSQL. So I am trying to write scripts by looking at some tutorials. But only few of them are helpful. Now I am trying to write a script for a bot which retrieves information from MYSQL database. But i dont know how to connect PHP to it.

Please help me.

<?php

$host = “???”; $account = “???”; $password = “???”; $dbname = “???”;

mysql_connect($host, $account, $password) or die("Couldn't connect to SQL Server on $host");
mysql_select_db($dbname) or die("Couldn't open database $dbname");

$sql = "SELECT * FROM "; $result = mysql_query($sql);

$row = mysql_fetch_array($result); echo( $row['field1'] ); echo('br'); echo( $row['feild2'] ); echo('br'); echo( $row['field3] ); echo('br'); echo( $row['field4'] );

?>

Is the script correct?
If yes, what values should i put for $host, $account, $password, $dbname.
I hv chosen http://www.blackapplehost.com as host.

  1. 2 Posted by mpahic on 31 Jul, 2009 01:24 PM

    mpahic's Avatar

    The script looks fine. To connect you need to place host, pass etc of your mysql server in there (not your host). Your hosting should provide you with this.

    Why don't you also add die() to the $result to see if your request has passed. Eg:
    $result = mysql_query($sql) or die("Error"); What do you receive?

  2. 3 Posted by Pedro Cassian on 11 Aug, 2010 03:24 AM

    Pedro Cassian's Avatar

    Im trying to my make life easier and upgrade my bot to mysql,

    currently i use php to search for keywords in what the post says and replies, but i had to make the code for each keyword and each reply, needless to say it wasn't very practical

    So, I got a mysql database running now and its configured correctly to connect, but im not sure how to make it search in a field for the keywords

    my old code is the following

    if (stristr($_POST['msg'], 'hi')) {

       print 'Hi, how are you?';

    }

    And i edited the php from the earlier post to make this

    $sql = "SELECT * FROM bot "; $result = mysql_query($sql);

    $row = mysql_fetch_array($result); echo( $row['output'] ); ?>

    however, it doesn't search within the field and thats the part im not to sure how to do

    any help would be appreciated, thanks!

  3. 4 Posted by Pedro Cassian on 11 Aug, 2010 03:46 AM

    Pedro Cassian's Avatar

    Ok, so after trying a bit, this is what i got, but it doesnt work (yet)


    <?php

    //DB CONFIG INFO

    $sql_res=mysql_query("SELECT * FROM bot WHERE input like '%$_POST%'"); while($row=mysql_fetch_array($sql_res))
    { $input=$row['input']; $output=$row['output'];

    ?>

    <?php echo $output; ?>

    however i get an error, im sure something is not right

    Parse error: syntax error, unexpected $end in /home/dlusionz/public_html/iteso.php on line 20

  4. 5 Posted by Pedro Cassian on 11 Aug, 2010 05:11 AM

    Pedro Cassian's Avatar

    okay, so I kinda got it working, however, I lost the functionality of SEARCHING the $_POST for the keywords, currently the user needs to type the exact keyword for it to work, heres the code im using, but I would like to know how I can regain that functionality

    if($_POST)
    {

    $q=$_POST['msg'];

    $sql_res=mysql_query("SELECT * FROM bot WHERE input like '%$q%'"); while($row=mysql_fetch_array($sql_res))
    { $output=$row['output']; $url=$row['url'];

    ?> <?php
    echo $output;
    echo "

    ";
    echo $url;
    ?>

    <?php
    }

    } else
    { echo "perdon, no entendi lo que escribiste, tal vez si lo escribes de otra manera te puedo ayudar";

    }

    ?>


    the idea of the ELSE statement is for the bot to say something if it doesnt find a word, however its currently not working, but that is secondary to my initial problem

    help would be appreciated, I don't think I cant get further than this

Reply to this discussion

Internal reply

Formatting help or Preview

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.