Jump to content

connecting to database with mysql


chris93

Recommended Posts

im trying to connect to database with mysql .

 

when i run the code im getting

 

Parse error: syntax error, unexpected end of file in C:\xampp2\htdocs\tutorials\ab1.php on line 22

 

 

<?php
$username = 'root';
$pass = 'fireman9';

$db = 'testdb';

$link = mysql_connect('localhost',$username,$pass);

mysql_select_db("testdb",$con);

if(!link){
echo "Not Connected to DB" . mysqli_connect_error();

$result = mysql_query($link, "SELECT * FROM test");

while($row = mysql_fetch_array($result)){
echo "ID: " . $row['id'];
echo "Name: " . $row['name33'];

}

?>

Link to comment
Share on other sites

Agreed, using the original php mysql extension in the OP's example leads you exposed to SQL injections. The new mysqli extension the QuickOldCar has stated for you provide a way of using prepared statements/parameterized statements and thus removes the risk of SQL injection. PDO is also another avenue you could look down :)

Link to comment
Share on other sites

Just to clarify, the real reason to switch to MySQLi or PDO is that the mysql_* functions have been depreciated. More information can be found here:

http://www.php.net/manual/en/mysqlinfo.api.choosing.php

 

If you're not quite ready to make the switch, you can prevent SQL injections with mysql_real_escape_string().

http://www.php.net/mysql_real_escape_string

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.