Jump to content

Get data from MySQL using PHP


kleave

Recommended Posts

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

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);

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

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.

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);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.