#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2

    Select data from mysql


    Hi,

    1. I have inserted a row in users table as

    insert into users
    values ( 1 , 'shams' , 'pw1' , 'hh@hero.com' )

    2. on my site, i entered user name as 'shams' and password as 'pw1' then click login.

    Question : I can connect to db just fine but its not printing the result(please see last line of code) after i enter shams and pw1 as username and password. Please help.



    <?php

    session_start();


    //This displays your login form




    echo "<form action='?act=login' method='post'>"

    ."Username: <input type='text' name='username' size='30'><br>"

    ."Password: <input type='password' name='password' size='30'><br>"

    ."<input type='submit' value='Login'>"

    ."</form>";




    $username = $_REQUEST['username'];

    $password = $_REQUEST['password'];
    //echo $username;

    $con = mysql_connect("host","db","7hhg3@");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    else
    {
    echo "con";
    }

    $result = mysql_query("SELECT id FROM users");
    echo $result;


    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    1) Please enclose your code in [ PHP ] tags (see the sticky at the top of this forum).
    2) Do not use the deprecated mysql extensions. Switch to PDO.
    3) Like PDO the query returns a result object not the row data itself. You need to do a 'fetch' or 'fetchall' on that object.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    Hi,

    I am not sure how to use the PHP tag. I could not find the instructions. Please provide.

    I got some improvement. Below code does not give me any error. but it does not print anything inside the while loop even though i have data in the table.

    Please help.


    <?php
    $con = mysql_connect("","","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    $query = "SELECT * FROM users";
    $result = mysql_query($query);

    while($row = @mysql_fetch_array($result))
    {
    echo $row['username'];
    echo "rr";
    echo "<br />";
    }

    mysql_close($con);
    ?>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    You highlight your code then click the PHP icon.

    I rarely do this but in the spirit of Christmas, here is what you need:
    PHP Code:
    <?php
    try {
       
    $conn=new->PDO("mysql:host=$host;dbname=$db",$user,$pass);
    }
    catch (
    PDOException $e) {
       die(
    "Connection failure: ".$e->getMessage());
    }

    $query "SELECT * FROM users";
    $result $conn->query($query);

    foreach(
    $result as $row)
    {
       echo 
    $row['username'];
       echo 
    "rr";
       echo 
    "<br />";
    }
    ?>
    Of course you will need to set $host, $db, $user and $pass as appropriate.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    Merry Christmas!!

    Many thanks for your time but my problem is not solved yet tho. I am getting error message:

    Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting T_STRING or T_VARIABLE or '$' in /home/content/ .......................... on line 7


    PHP Code:
    <?php 
    $host 
    "......" // i copied everything under hostname in here
    $db "......." // name of the database
    $user "......" ;  //db name and user name same
    $pass "......"//pass goes here

    try { 
    [
    B$conn=new->PDO("mysql:host=$host;dbname=$db",$user,$pass);  [/B]  // line 7

    catch (
    PDOException $e) { 
       die(
    "Connection failure: ".$e->getMessage()); 


    $query "SELECT * FROM users"
    $result $conn->query($query); 

    foreach(
    $result as $row

       echo 
    $row['username']; 
       echo 
    "rr"
       echo 
    "<br />"

    ?>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    Oops. Sorry about that.
    PHP Code:
    $conn=new PDO("mysql:host=$host;dbname=$db",$user,$pass); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    It was very nice of you. It worked just fine.

    If you dont mind, could you please tell me why my code does not print out anything in while loop. I dont also get any error.

    PHP Code:
    <?php
    $con 
    mysql_connect("","","");
    if (!
    $con)
    {
    die(
    'Could not connect: ' mysql_error());
    }

    $query "SELECT * FROM users";
    $result mysql_query($query);

    while(
    $row = @mysql_fetch_array($result))
    {
    echo 
    $row['username'];
    echo 
    "rr";
    echo 
    "<br />";
    }

    mysql_close($con);
    ?>
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    That is not the code I gave you and I don't use obsolete extensions.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    2
    The code you have is not printing anything because you're suppressing errors and you're treating $row as an assoc when it's not.

    Although you shouldn't be using this code at all, here is the working version:

    PHP Code:
    <?php 
    $con 
    mysql_connect("","",""); 
    if (!
    $con

    die(
    'Could not connect: ' mysql_error()); 


    $query "SELECT * FROM users"
    $result mysql_query($query); 

    while(
    $row mysql_fetch_assoc($result)) 

    echo 
    $row['username']; 
    echo 
    "rr"
    echo 
    "<br />"


    mysql_close($con); 
    ?>
    I strongly recommend you to use PDO, as the nice chap gw1500se has demonstrated for you.

    If you find it hard to understand PDO, you haven't grasped the basics. Do some research on Object-Oriented Programming. There's so much info out there. You just need to take the time to find it.

    Bad habits are hard to forget. You've been warned. If you're going to be stupid enough to deliberately pick up bad habits, without good reason, you may as well give up now.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    Many thanks for your help!

IMN logo majestic logo threadwatch logo seochat tools logo