Having PHP Issues with Displaying SQL Data on a Dynamic Web Page.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1330

    #1

    Tech Having PHP Issues with Displaying SQL Data on a Dynamic Web Page.

    Any programmers able to see what I'm fucking up?

    This is for a personal project I'm working on.

    I should be able to open index.php to display a listing of 'recipes' in the database which will allow me to click through to viewmore.php where i have more information displayed on the page.

    PHP Code:
    <?php
    
    
    // change the user_name and password
    $db = mysqli_connect("localhost", "dbuser", "dbpass");
     
    // change the database_name
    mysqli_select_db("dbname",$db);
    
    $id = $_GET['id'];
    
    $result = mysqli_query($con,"SELECT RecipeID FROM Recipes WHERE id = $id");
    
    echo "<table width=100%>
    <tr>
    <th>ID</th>
    <th>Title</th>
    <th>Ingredients</th>
    <th>Method</th>
    <th>Keywords</th>
    </tr>";
    
    while($row = mysqli_fetch_array($result))
    {
    
    $id = $row['RecipeID'];
    
    echo "<tr>";
    echo "<td>" . $row['RecipeID'] .  "</td>";
    echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>";
    echo "<td>" . $row['Ingredients'] .  "</td>";
    echo "<td>" . $row['Method'] . "</td>";
    echo "<td>" . $row['Keywords'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    
    ?>
    Which should, allow me to generate a link to viewmore.php?id=46 to show the data in the sql for that row, correct?

    This is my viewmore.php page code...

    PHP Code:
    <?php
    
    $id = $_get['id'];
    
    $result = mysqli_query($con,"SELECT * FROM Recipes WHERE RecipeID='$id'");
    
    echo "<table width=100%>
    <tr>
    <th>Title</th>
    <th>Ingredients</th>
    <th>Method</th>
    <th>Keywords</th>
    </tr>";
    
    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td> <a href='#'>" . $row['Title'] . "</a> </td>";
    echo "<td>" . $row['Ingredients'] .  "</td>";
    echo "<td>" . $row['Method'] . "</td>";
    echo "<td>" . $row['Keywords'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    
    ?>
    I'm sure its a simple issue that I'm missing but I've been up almost 48 hours and my mind just isn't seeing it :/

    Any help would be greatly appreciated.
    Extreme Link List - v1.0
  • Colmike9
    (>^_^)b
    • Dec 2011
    • 7230

    #2
    // change the user_name and password
    $db = mysqli_connect("localhost", "dbuser", "dbpass");

    // change the database_name
    mysqli_select_db("dbname",$db);


    Did you put in the right info?
    Join the BEST cam affiliate program on the internet!
    I've referred over $1.7mil in spending this past year, you should join in.
    I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..

    Comment

    • Publisher Bucks
      Confirmed User
      • Oct 2018
      • 1330

      #3
      Originally posted by Colmike9
      // change the user_name and password
      $db = mysqli_connect("localhost", "dbuser", "dbpass");

      // change the database_name
      mysqli_select_db("dbname",$db);


      Did you put in the right info?
      Yes, that was the first thing I checked as previously I've spent hours trying to debug something only to realize I made a typo on the username lol

      I do know my host just recently upgraded a bunch of php so I also have a ticket in with them to make sure they didn't fuck anything up (its Hostgator who I use for my personal stuff).
      Extreme Link List - v1.0

      Comment

      • Way3
        Confirmed User
        • Mar 2007
        • 1727

        #4
        I don't think it is making the database connection in the viewmore.php or the index.php file.

        I don't believe the $con has actually been defined for the db connection.

        Just my 2 cents.

        FULLY Managed! FREE Control Panel! FREE Month on ALL Shared Accounts!!!
        ICQ: 169-554-261
        Email: info[at]way3{dot}com

        Comment

        • Publisher Bucks
          Confirmed User
          • Oct 2018
          • 1330

          #5
          Originally posted by Way3
          I don't think it is making the database connection in the viewmore.php or the index.php file.

          I don't believe the $con has actually been defined for the db connection.

          Just my 2 cents.
          Doesnt mysqli_connect establish that?
          Extreme Link List - v1.0

          Comment

          • k0nr4d
            Confirmed User
            • Aug 2006
            • 9231

            #6
            Originally posted by Publisher Bucks
            Doesnt mysqli_connect establish that?
            Sure, if the info is correct

            https://www.php.net/manual/en/mysqli.connect-error.php
            Mechanical Bunny Media
            Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

            Comment

            • k0nr4d
              Confirmed User
              • Aug 2006
              • 9231

              #7
              Nevermind, I just actually looked at your code.
              You called your db connection $db but are called $con in your mysqli_query

              $con = mysqli_connect("localhost", "dbuser", "dbpass");
              mysqli_select_db("dbname",$con);

              You also need to establish the db connection on the viewmore.php file
              Mechanical Bunny Media
              Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

              Comment

              • sarettah
                see you later, I'm gone
                • Oct 2002
                • 14297

                #8
                This is just wrong unless I am really missing something.

                You select recipeid from the table but then you are trying to echo out variables before you have them. You are treating the href as if it is an include.


                while($row = mysqli_fetch_array($result))
                {

                // Here you pull in the ID field.
                $id = $row['RecipeID'];

                echo "<tr>";
                echo "<td>" . $row['RecipeID'] . "</td>";

                //Everything is fine till here.

                // You attempt to build your href but it isnt going to work because
                // you attempt to echo out title when you don't have the title yet.

                echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>";

                // Then you attempt to echo out the other fields when, again, you don't have them yet.

                echo "<td>" . $row['Ingredients'] . "</td>";
                echo "<td>" . $row['Method'] . "</td>";
                echo "<td>" . $row['Keywords'] . "</td>";
                echo "</tr>";
                }
                echo "</table>";

                .................................................. ......................

                Try something like this instead:


                $result = mysqli_query($con,"SELECT RecipeID, Title FROM Recipes WHERE id = $id");

                echo "<table width=100%>
                <tr>
                <th>ID</th>
                <th>Title</th>
                </tr>";


                while($row = mysqli_fetch_array($result))
                {

                $id = $row['RecipeID'];

                echo "<tr>";
                echo "<td>" . $row['RecipeID'] . "</td>";
                echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>";
                echo "</tr>";
                }
                echo "</table>";

                Then, in viewmore, as k0nr4d said, you need to establish a connection.

                Not sure I caught all of it but that is the way I see it, like I said, unless I missed something somewhere.

                .
                All cookies cleared!

                Comment

                • Publisher Bucks
                  Confirmed User
                  • Oct 2018
                  • 1330

                  #9
                  Appreciate the help guys, going to play around a little and see if I can put a working code together based on your feedback, thank you!
                  Extreme Link List - v1.0

                  Comment

                  • Klen
                    • Aug 2006
                    • 32235

                    #10
                    It seems i was too late on party :D Still, always check how your variables contain data/functions.

                    Comment

                    • Publisher Bucks
                      Confirmed User
                      • Oct 2018
                      • 1330

                      #11
                      Ive edited the code as suggested for both pages and now its throwing out a blank page with the 'ID' and 'Title' table but no data being inserted, i checked the error logs and its showing me this:

                      [02-Aug-2021 14:30:31 America/Chicago] PHP Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home2/blah/blah.com/index.php on line 6
                      [02-Aug-2021 14:30:31 America/Chicago] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in /home2/blah/blah.com/index.php on line 26
                      Here is the new code im using based on the feedback in this thread so far...

                      index.php

                      PHP Code:
                      <?php
                      
                      
                      // change the user_name and password
                      $con = mysqli_connect("localhost", "dbuser", "dbpass");
                      mysqli_select_db("dbname",$con);
                       
                      
                      $id = $_GET['RecipeID'];
                      
                      // Check connection
                      if (mysqli_connect_errno()) {
                        echo "Failed to connect to MySQL: " . mysqli_connect_error();
                        exit();
                      }
                      
                      $result = mysqli_query($con,"SELECT RecipeID, Title FROM Recipes WHERE id = $id");
                      
                      echo "<table width=100%>
                      <tr>
                      <th>ID</th>
                      <th>Title</th>
                      </tr>";
                      
                      
                      while($row = mysqli_fetch_array($result))
                      {
                      
                      $id = $row['RecipeID'];
                      
                      echo "<tr>";
                      echo "<td>" . $row['RecipeID'] . "</td>";
                      echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>";
                      echo "</tr>";
                      }
                      echo "</table>";
                      
                      ?>
                      and for the viewmore.php

                      PHP Code:
                      <?php
                      
                      // change the user_name and password
                      $con = mysqli_connect("localhost", "dbuser", "dbpass");
                      mysqli_select_db("dbname",$con);
                       
                      $id = $_GET['RecipeID'];
                      
                      // Check connection
                      if (mysqli_connect_errno()) {
                        echo "Failed to connect to MySQL: " . mysqli_connect_error();
                        exit();
                      }
                      
                      $result = mysqli_query($con,"SELECT RecipeID, Title FROM Recipes WHERE id = $id");
                      
                      echo "<table width=100%>
                      <tr>
                      <th>ID</th>
                      <th>Title</th>
                      </tr>";
                      
                      
                      while($row = mysqli_fetch_array($result))
                      {
                      
                      $id = $row['RecipeID'];
                      
                      echo "<tr>";
                      echo "<td>" . $row['RecipeID'] . "</td>";
                      echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>";
                      echo "</tr>";
                      }
                      echo "</table>";
                      
                      ?>
                      The page itself isn't throwing out any errors from the connection, and the ID and Title shows in the table but no data is being displayed.

                      The url im using is:

                      domain.com/index.php?id=X (where X is a valid RecipeID in the table)
                      domain.com/viewmore.php?id=X (where X is a valid RecipeID in the table)

                      I'm going to get a few hours sleep, but if ya'll happen to spot anything amiss it'd be appreciated.

                      Thanks again for the help so far
                      Extreme Link List - v1.0

                      Comment

                      • sarettah
                        see you later, I'm gone
                        • Oct 2002
                        • 14297

                        #12
                        mysqli_select_db("dbname",$con);

                        Should be mysqli_select_db($con,"dbname");

                        PHP.net is your friend.

                        https://www.php.net/manual/en/mysqli.select-db.php

                        Procedural style

                        mysqli_select_db(mysqli $mysql, string $database): bool
                        .
                        All cookies cleared!

                        Comment

                        • vdbucks
                          Monger Cash
                          • Jul 2010
                          • 2773

                          #13
                          Originally posted by Publisher Bucks
                          Any programmers able to see what I'm fucking up?

                          This is for a personal project I'm working on.

                          I should be able to open index.php to display a listing of 'recipes' in the database which will allow me to click through to viewmore.php where i have more information displayed on the page.


                          Which should, allow me to generate a link to viewmore.php?id=46 to show the data in the sql for that row, correct?



                          I'm sure its a simple issue that I'm missing but I've been up almost 48 hours and my mind just isn't seeing it :/

                          Any help would be greatly appreciated.
                          There are a handful of things wrong here...

                          1. Your stated goal is to display a list of recipes on index.php with a link to the individual recipe info on viewmore.php. The problem is, your index.php script is expecting a recipe ID to be passed as a query string, ala index.php?id=123.

                          2. Your SELECT query on index.php only ever asks for 'ReceipID' from the db, but you're attempting to display other data. Not to mention, you're trying to get the ReceipID from the db while also telling the db what the RecipeID is.

                          3. Your output on index.php and viewmore.php are effectively the same, assuming the queries worked.

                          4. You're using <table> in 2021.

                          At any rate, going by what your stated goal is, I think something like this is more in line with what you want:

                          Code:
                          <?php
                          /*** index.php ***/
                          
                          	// change hostname, username, password, and dbname
                          	$db = new mysqli( 'hostname', 'username', 'password', 'dbname');
                          	if ($db->connect_errno)
                          		die("Connect failed: %s\n", $db->connect_error);
                          	 
                          	$getRecipes = "SELECT ReceipID, Title FROM Recipes";
                          	$response = $db->query($getRecipes);
                          
                          	if ($response->num_rows) {
                          		echo "<table width=100%>
                          				<tr>
                          				<th>ID</th>
                          				<th>Title</th>
                          				</tr>";
                          
                          		$row = $db->get_results($response);
                          
                          		foreach($row as $key => $value) {
                          
                          			$id = $value->RecipeID;
                          			$title = $value->Title;
                          
                          			echo "<tr>";
                          			echo "<td>{$id}</td>"; // not sure why you want to display the recipe id, but okay.
                          			echo "<td> <a href=\"viewmore.php?id={$id}\">{$title}</a></td>";
                          			echo "</tr>";
                          		}
                          
                          		echo "</table>";
                          	}
                          
                          	$db->close();
                          ?>
                          Code:
                          <?php
                          /*** viewmore.php ***/
                          
                          	// change hostname, username, password, and dbname
                          	$db = new mysqli( 'hostname', 'username', 'password', 'dbname');
                          	if ($db->connect_errno)
                          		die("Connect failed: %s\n", $db->connect_error);
                          
                          	$id = $_GET['id'];
                          
                          	$getRecipe = "SELECT * FROM Recipes WHERE ReceipID = {$id}";
                          	$response = $db->query($getRecipe);
                          
                          	if ($response->num_rows) {
                          		echo "<table width=100%>
                          				<tr>
                          				<th>Title</th>
                          				<th>Ingredients</th>
                          				<th>Method</th>
                          				<th>Keywords</th>
                          				</tr>";
                          
                          		$row = $db->get_results($response);
                          
                          		foreach($row as $key => $value) {
                          			echo "<tr>";
                          			echo "<td>{$value->Title}</td>";
                          			echo "<td>{$value->Ingredients}</td>";
                          			echo "<td>{$value->Method}</td>";
                          			echo "<td>{$value->Keywords}</td>";
                          			echo "</tr>";
                          		}
                          		echo "</table>";
                          	}
                          
                          	$db->close();
                          ?>

                          Comment

                          • sarettah
                            see you later, I'm gone
                            • Oct 2002
                            • 14297

                            #14
                            LOL, miss a lot of stuff when you move too fast.

                            .
                            All cookies cleared!

                            Comment

                            • Klen
                              • Aug 2006
                              • 32235

                              #15
                              Originally posted by sarettah
                              mysqli_select_db("dbname",$con);

                              Should be mysqli_select_db($con,"dbname");

                              PHP.net is your friend.

                              https://www.php.net/manual/en/mysqli.select-db.php



                              .
                              Yep, php.net is still one of most valuable resources, tho funny thing is how solution is often found in comments, not in official entry.

                              Comment

                              Working...