kleave Posted June 2, 2008 Share Posted June 2, 2008 Hi, I new to PHP and I not know much about PHP hope to learn something from this forum. I develop this simple php script for my understanding. This script is to get the data from MySQL database but somehow it did not get the data. From what I know is that 1)Create a connection to MySQL 2)Select database 3)Create query 4)Read Extracted data later on i will use the result for other purpose but somehow it did not get data from database. can tell me which part I do wrong? <?php $conn = mysql_connect("localhost") or die("Error: Cannot connect to database"); mysql_select_db("shipping"); $sql= "SELECT price FROM product WHERE product = 'Adobe'"; $ps = mysql_query($sql,$conn); $result = mysql_fetch_row($ps); echo($result); ?> Link to comment https://forums.phpfreaks.com/topic/108342-get-data-from-mysql-using-php/ Share on other sites More sharing options...
DarkerAngel Posted June 2, 2008 Share Posted June 2, 2008 That's because the data is returned in an array, and you wouldn't be able to output the info using echo, try replacing echo with print_r and see if you get any output. Link to comment https://forums.phpfreaks.com/topic/108342-get-data-from-mysql-using-php/#findComment-555437 Share on other sites More sharing options...
kleave Posted June 2, 2008 Author Share Posted June 2, 2008 thank for reply i have change is still the same, this is the error msg i get "Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\calculate.php on line 11" May I know the different between echo & print? Link to comment https://forums.phpfreaks.com/topic/108342-get-data-from-mysql-using-php/#findComment-555439 Share on other sites More sharing options...
DarkerAngel Posted June 2, 2008 Share Posted June 2, 2008 echo outputs a string print works the same way, but there are differences print_r (as defined in the PHP manual): Prints human-readable information about a variable as for your error the mysql_query() probably was not successful, try modifying it to: mysql_query($sql, $conn) or die("MySQL Error: ".mysql_error()."<br />Query was: ."$sql); Link to comment https://forums.phpfreaks.com/topic/108342-get-data-from-mysql-using-php/#findComment-555442 Share on other sites More sharing options...
RMcLeod Posted June 2, 2008 Share Posted June 2, 2008 Change the mysql_query line to the following <?php $ps = mysql_query($sql,$conn) or die(mysql_error()); ?> This will tell you if there is a problem with your query, which it looks like there is. Don't confuse print and print_r they are different. There is no difference between print and echo, print_r is used to display human readable information about a variable (usually an array). EDIT: ^^ Beat me to it Link to comment https://forums.phpfreaks.com/topic/108342-get-data-from-mysql-using-php/#findComment-555444 Share on other sites More sharing options...
kleave Posted June 2, 2008 Author Share Posted June 2, 2008 hi, Thank for your help Now I know the error msg , this is updated one "MySQL Error: No database selected" how come it did not select the database whereby I include this line of code. "mysql_select_db("shipping");" Thank, now I learn something new about the mysql_error. Link to comment https://forums.phpfreaks.com/topic/108342-get-data-from-mysql-using-php/#findComment-555449 Share on other sites More sharing options...
DarkWater Posted June 2, 2008 Share Posted June 2, 2008 Verify that the database exists. Link to comment https://forums.phpfreaks.com/topic/108342-get-data-from-mysql-using-php/#findComment-555459 Share on other sites More sharing options...
kleave Posted June 3, 2008 Author Share Posted June 3, 2008 I have checked the database and is there. Anyone know the problem? Thank you...... This is updated code. <?php $conn = mysql_connect("localhost") or die("Error: " . mysql_error()); mysql_select_db("shipping", $conn); $sql= "SELECT price FROM product WHERE product = Adobe"; $result = mysql_query($sql, $conn) or die("MySQL Error: ".mysql_error()." <br>Query was: ".$sql); print_r($result); ?> Link to comment https://forums.phpfreaks.com/topic/108342-get-data-from-mysql-using-php/#findComment-556126 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.