Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    Morley, UK
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    PHP Very Basic Select Statement for MYSQL

    Hi Guys

    I've checked the forum for anything similar in previous posts but couldn't find anything
    I've also read at w3schools, but that didn't help

    What I want is a really basic select statement that only selects one field

    The example on w3schools (and many others on the web) starts with
    select 2 fields


    all I want it to do is display a list of clubs from tblclubs (there are only 4)

    Within the mysql table is this:
    tblclubs
    c_id
    c_name
    c_address
    c_stylesheet


    PHP Code:
    <?php
    $result 
    mysqli_query($con,"SELECT * FROM tblclubs");
    while(
    $row mysqli_fetch_array($result))
      {
      echo 
    $row['c_name'] ;
      echo 
    "<br />";
      }
    mysqli_close($con);
    ?>
    I get these errors
    Code:
    Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli in /home/a9629301/public_html/php/getclubs.php on line 2
    
    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/a9629301/public_html/php/getclubs.php on line 3
    
    Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch mysqli in /home/a9629301/public_html/php/getclubs.php on line 8
    All i want to to do is display:
    club1
    club2
    club3
    club4

    Any guidance would be welcome

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    First, provide the query with what you need instead of wildcarding it. You should always do this instead of requesting for *. In this case all you need is SELECT c_name FROM tblclubs ORDER BY c_name ASC. Note that you should also provide an order by all the time; DBMS provide absolutely no guarantee in an order unless you have specified it.

    Next, mysql and mysqli libraries are different beasts. The problem here is lack of error checking; the $con isn't established as a valid mysqli object, so each call from mysqli_query on will fail. Where have you constructed the $con variable?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Nov 2011
    Location
    Morley, UK
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hi Fou-Lu

    So when you say literally you mean all i need is
    PHP Code:
    <?php
    $result 
    mysqli_query("SELECT c_name FROM tblclubs ORDER BY c_name ASC");
    ?>
    As it's giving:
    Code:
    Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/a9629301/public_html/php/getclubs.php on line 2
    Apologies if I'm being simple and taking what you have said "too" literally

    The $con variable is established in a seperate php file called connections.php
    and is called on my web page via <?php include 'php/connections.php';?>

    That way, (have the $con is an seperate file)
    seems to have worked for a similar query is which it displays all members from tblmembers (not sure if that is relevant)

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, that's all you need for the query. You still need an established connection to work with in mysqli_query function call.
    According to your error, $con is not available in scope of where you have called the mysqli_query. Change your include to a require_once call.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    stuckupvacant (03-22-2013)

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    your 1st post is correct and will work fine,

    you just havent opened the database first

    PHP Code:

     <?php
    $con 
    mysqli_connect("localhost""user""password""database");
    $result mysqli_query($con,"SELECT * FROM tblclubs");
    while(
    $row mysqli_fetch_array($result))  {
      echo 
    $row['c_name']."<br />" ;
    }
    mysqli_close($con);
    ?>
    Last edited by Arcticwarrio; 03-22-2013 at 05:01 PM. Reason: sorry posted class connection
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    stuckupvacant (03-22-2013)

  • #6
    New Coder
    Join Date
    Nov 2011
    Location
    Morley, UK
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Just reading up on the include vs require

    (http://www.w3schools.com/php/php_includes.asp)

    should I have the require on the html page or put it in the php file?

  • #7
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    directly above this line

    $result = mysqli_query($con,"SELECT * FROM tblclubs");
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #8
    New Coder
    Join Date
    Nov 2011
    Location
    Morley, UK
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks Guys that has worked

    Can it be done though, so that i only ever have one php file hold the user/pass and connects to the DB ?

    so in this case connections.php
    (this holds the DB use, pass)

    and call that within other php files?

  • #9
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Yes that is a best practice. Just put the database connection into a connection.php or whatever you see fit, cram it into an includes folder, and then when you need to access the database just require it. This means when you alternate between local and launching it you only have to change the username / password in one place!

    Code:
    require_once('includes/connection.php');

  • #10
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    or you could do it like this

    PHP Code:

    <?php
    $con 
    mysqli_connect($server$user$password$database);
    $result mysqli_query($con,"SELECT * FROM tblclubs");
    while(
    $row mysqli_fetch_array($result))  {
      echo 
    $row['c_name']."<br />" ;
    }
    mysqli_close($con);
    ?>
    then just have this in an included file

    PHP Code:
    $server "localhost";
    $user "user";
    $password "password";
    $database "database"
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #11
    New Coder
    Join Date
    Nov 2011
    Location
    Morley, UK
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hi Arctic

    So is the include file a seperate php file that would sit in a folder called include?

    if so how does
    $con = mysqli_connect($server, $user, $password, $database);
    know to point to "include.php"

    The way KULP suggeted with using:
    require_once('includes/connection.php');makes sense as I can see that, it is "physically" (in code anyway) pointing to the file in the "include" folder
    Last edited by stuckupvacant; 03-27-2013 at 12:20 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •