0

I am trying to make a blog site.For this purpose I need to use a specific data from a specific field from my database table.To do that I wrote these code.

    <?php
    $host = "localhost";
    $user = "root";
    $pass = "12345";
    $db = "bnsb";
    $conn = mysql_connect($host, $user, $pass) or die("Connection Failed!");
    mysql_select_db($db, $conn) or die("Database couldn't select!");

    $img = "select image from news where uid=1";
    echo $img;
    ?>

My database connection is OK.It should print like this user_img1.jpg. But it prints the whole sql query like select image from news where uid=1. I run this code on phpmyadmin. It works! But it does not work in my php script.How can I do now?

4
  • you have to execute the query and get the result and then print it.but as of now you are just printing a string Commented Feb 11, 2014 at 6:44
  • 1
    connect to your db... select your db... run the $img query... get the value of the result... and you are happy Commented Feb 11, 2014 at 6:45
  • 1
    Heres a good starting point for you, showing how to use mysqli_* functions in php, from connecting to the database to executing your query and dealing with the result set; pontikis.net/blog/… - hope this helps :) Commented Feb 11, 2014 at 6:47
  • @user3167680 again you are trying to print only the query without executing it.Please execute it and get the result and print that result Commented Feb 11, 2014 at 7:04

3 Answers 3

3

You can not give the query as it is and expect result like in phpadmin. For this first of all you have to connect to your DB like this

$con = mysqli_connect("localhost","my_user","my_password","my_db");

execute required query like this

$query22 = "select image from news where uid = 1";

$result22 = mysqli_query($con, $query22) or die (mysqli_error());

Get the result and display like this

while($rows = mysqli_fetch_array($result22, MYSQLI_BOTH)) 
{
  echo "<br>Values in db: " . $rows['columnname'];
}

Also i advice you to take a look at these tutorials

http://codular.com/php-mysqli

http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks krishna. I have tried like you said. but there was an error message. "Query was empty". @krishna
can you post the code which you tried ? Mainly the querys and execution part
<?php $host = "localhost"; $user = "root"; $pass = "12345"; $db = "bnsb"; $conn = mysql_connect($host, $user, $pass) or die("Connection Failed!"); mysql_select_db($db, $conn) or die("Database couldn't select!"); $img = "select image from news where sn=1"; $result=mysql_query($conn,$img); $row=mysql_fetch_array($result); echo $row['image']; if(mysql_query($result)) { echo "congratulations!"; } else { die(mysql_error()); } ?> The result is "Query was empty" @krishna
the problem is you are mixing mysqli syntax with mysql syntax. change mysql_query($conn,$img); to mysql_query($img,$conn);
i also advice you to use mysqli_* functions
|
1

Please read some PHP 101 kind of tutorials on how to use PHP.

To get data from DB (in almost any language)

  1. You need to connect to a DB. The connection gets you some sort of resource
  2. You formulate your query (which you seem to have done)
  3. You execute the query against the DB that you connected to (step #1)
  4. You get a result (set)
  5. You iterate over the result set to get the individual result(s); in your case the result set would be just one result (or row).

The examples to do this in PHP are very basic; please do your own lookup on net. This one seems good enough to get you started - http://www.w3schools.com/php/php_mysql_intro.asp

1 Comment

Thanks. I m trying for two days in various websites and books included(w3schools). But have not got any solution yet.So I m asking here.For your kind information, My database connection is OK.Thanks again.:-)
0

Try this,

<?php
$host = "localhost";
$user = "root";
$pass = "12345";
$db = "bnsb";
$conn = mysql_connect($host, $user, $pass) or die("Connection Failed!");
mysql_select_db($db, $conn) or die("Database couldn't select!");

$img = "select image from news where uid=1";
$result=mysql_query($img);
while($row=mysql_fetch_array($result)){
   echo '<img src="your_path_to_image/'.$row['image'].'" /> - '.$row['image'];
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.