Post: [MySQL] Calling Rows
08-31-2013, 01:59 AM #1
Dan
I'm a god.
(adsbygoogle = window.adsbygoogle || []).push({}); This tutorial is not for PHP newbs, it is not advanced either. With this you would be able to call the content in specific rows in your MySQL table. I'm not going to go into depth about this.

First off you want to make your MySQL connection (Yes, I'm using the old mysql functions.):
    
<?php
mysql_connect("localhost", "db_user", "db_user_password");
mysql_select_db("your_db_name");
?>


Now you're gonna start your query, to call everything from the table you use the asterisk (*):
    
<?php
$query = "SELECT * FROM table_name";
$result = mysql_query($query);
?>


And now comes the part where you will call the row you want to echo out:
    
<?php
while($row=mysql_fetch_array($result))
{
$title = $row['title'];
$name = $row['body'];
}
?>


To call the row you want, as you can see you use the while function and declare $row variable. The mysql_fetch array function returns an array of strings that corresponds to the fetched row. Within the while loop you define which rows you want to fetch, you can name the variables anything you want, but when setting them you will need to use the variable you used as $row. When you want to echo the contents of the row in some HTML, it would look like so:

    
<html>
<body>
<?php echo $title . "<br>" . $name; // For the variables to be echo'd you have to concatenate them.
</body>
</html>


Thanks for taking the time to read my short tutorial. Sorry if I missed explaining something or I didn't go into detail enough for you to understand it. I have a hard time with explaining things and hope to get better when making future tutorials. :y:
Last edited by Dan ; 08-31-2013 at 03:14 PM.
08-31-2013, 03:16 PM #2
Dan
I'm a god.
Originally posted by Woof
I would personally suggest always trying to avoid using an asterisk, because it's much more efficient not to for when you extend another row in your table, etc. I would also suggest using prepared statements too for the security and performance increase when calling a query frequently. Prepared statements improve performance dramatically if you're using 'mysql_real_escape_string' as this actually communicates with the database itself. I've recently written a thread on prepared statements:
You must login or register to view this content.

Also you haven't actually defined $result, and I noticed a syntax in your last snippet. Here is what I would use:
    <?php echo $title.'<br>'.$name; ?>


Thanks, fixed it.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo