# MySQL and PHP

**URL:** <https://forum.kirupa.com/t/mysql-and-php/263305>\
**Category:** Uncategorized\
**Created:** [June 14, 2008, 8:16am UTC](https://forum.kirupa.com/t/mysql-and-php/263305 "2008-06-14T08:16:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smith\_myles](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@smith\_myles](https://forum.kirupa.com/u/smith_myles)\
**Post date:** [June 14, 2008, 8:16am UTC](https://forum.kirupa.com/t/mysql-and-php/263305/1 "2008-06-14T08:16:21Z")

</div>

Hi,

Do you know if it is possible fetch a MySQL record and convert each field into a PHP variable. I ask because I am building a user login system with user pages etc for my website, which you can view [here](http://nardoo.co.uk/justfloating). (user=test1, pass=demouser)

Any help would be great

Thanks

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [June 14, 2008, 5:15pm UTC](https://forum.kirupa.com/t/mysql-and-php/263305/2 "2008-06-14T17:15:48Z")

</div>

```php

$sql = "SELECT somecolumn FROM sometable WHERE someothercolumn = '$somePHPVariable'";
$result = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($result);
if($result)
{
     for($i=0; $i<$count; $i++)
     {
          $row = mysql_fetch_array($result);
          extract($row); 
          //creates individual php variables 
           //named after the column names in mySQL table
     }
}

```

---

<div class="post-metadata">

**Author:** ![smith\_myles](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@smith\_myles](https://forum.kirupa.com/u/smith_myles)\
**Post date:** [June 14, 2008, 9:24pm UTC](https://forum.kirupa.com/t/mysql-and-php/263305/3 "2008-06-14T21:24:53Z")

</div>

Thank you very much. Works perfectly.

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [June 15, 2008, 5:49pm UTC](https://forum.kirupa.com/t/mysql-and-php/263305/4 "2008-06-15T17:49:34Z")

</div>

One thing I want to mention - Always use the function mysql\_real\_escape\_string to sanitize any input going into the query.

---

<div class="post-metadata">

**Author:** ![smith\_myles](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@smith\_myles](https://forum.kirupa.com/u/smith_myles)\
**Post date:** [June 15, 2008, 7:54pm UTC](https://forum.kirupa.com/t/mysql-and-php/263305/5 "2008-06-15T19:54:53Z")

</div>

Thanks.

I have another issue now. Do you know how to get data from a mysql, put it into a form so it can be edited, and then re submitted to the mysql. Again only editing the one record defined by a php variable?

I have worked on it all day and i’m at odds with it.

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [June 16, 2008, 2:07am UTC](https://forum.kirupa.com/t/mysql-and-php/263305/6 "2008-06-16T02:07:41Z")

</div>

I’m going to assume that you know how to get the info into the form in the first place, since your original question was on retrieving. What I do is create a single function that I use for creating and updating. For example:

```php

function form($update=false, $formValues=ARRAY())
{
     //Check to see if it is a new entry or an edit
      ($update == false) ? $submitName = "submitNew" : "submitEdit";
 
     //Check to see if the $formValues array has info
     if(count($formValues) > 0) { extract($formValues); }
 
     //Now, create the html for the form
     $form = '<form method="POST" value="'.$_SERVER['REQUEST_URI'].'"><br />';
     $form .= "Name: <input type=\"name\" value=\"$name\" /><br />";
     $form .= "Something: <input type=\"something\" value=\"$something\" /><br />";
     $form .= "Something Else: <input type=\"somethingelse\" value=\"$somethingelse\" /><br />";
     $form .= "<input type=\"submit\" value=\"Submit\" name=\"$submitName\" /></form><br />";
     echo $form;
}
 
//then, you can create two functions to handle the creation 
//of the record and the updating of the record.
 
function create()
{
     if(isset($_POST['submitNew']))
     {
          //validate input and insert into the database
     }
     else
     {
          echo "Fill out the form to create a new record";
          form();
     }
}
 
function update()
{
     if(isset($_POST['submitEdit']))
     {
          //validate input and update DB
     }
     else
     {
          //Select record from database and save data 
          //into an array named $row for instance
          form(true, $row);
     }
}
 

```
