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
https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/
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 :)

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.