How to link mysql database with php


#1

I’m fairly new to php and MySQL, so sorry if my question seems a little vague. I looked at many forums and everyone seems to have a different way of doing it. None of them work for me. I got my code from w3schools, but it has an error on the try line.
The error says:
"Parse error: syntax error, unexpected ‘$som’ (T_VARIABLE) in /storage/ssd4/647/2267647/public_html/dbinfo.php on line 14"
W3Schols gives examples for both PDOs and MySQLi which I don’t totally understand.
Is there anyway I can alter my code to make this work, or is there something I’m fundamentally not understanding about PHP and MySQL?

Here is a portion of my code:

try {
    $som = new PDO("mysql:host=$hostname;dbname=myDB", $dbUserName, $dbPassword);
    $som->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
?>

#2

@ckhawand

w3schools typically provides the best and most updated code. However, it’s looking to me like you haven’t defined the variable som. The snippet you included about connecting seems okay, but I think @ckhawand or @NGiNX could verify.


#3

Hi @loganheller!

To be honest, I have never worked with PHP PDO, nor with try{} catch{} keywords. Therefore I am unable to debug your code.

I suggest you to use the procedural mysqli_* instead, if you are not familiar with classes and methods yet. Below I have written you an example.

$host = 'xxx';
$username = 'xxx';
$password = 'xxx';
$database = 'xxx';

$connection = mysqli_connect($host, $username, $password, $database); // connect and select the database at the same time;

if(!$connection)  // prints connection error if any
{
  print(mysqli_connect_error());
}

$query = mysqli_query($connection, "SELECT * FROM xxx"); // the query to execute; result will be assigned as object to $query

if(!$query)  // prints query error if any
{
   mysqli_error($connection);
}

mysqli_close($connection);  // disconnects $connection from MySQL server

#4

I got an error on the query part. My guess would be because there is not table created to query. How can I create tables and records through PHP code? I am pretty sure The “mysqli_connect” line worked to connect php and mysql. But on the (“SELECT * FROM xxx”) statement, is the “xxx” supposed to be a table name. If so how do I create a table? Do I have to do it manually from phpmyadmin?


#5

Do I have to do it manually from phpmyadmin?

Yes, from there it would be the easiest way :wink:


#6

I replaced the “xxx” with the table I created and I got the same error as I did before I created the table:
Warning: mysqli_query() expects at least 2 parameters, 1 given in /storage/ssd4/647/2267647/public_html/dbinfo.php on line 18


#7

True. I forgot to add the connection on which the query should be executed. Sorry about that.

This is the correct code:


#8

Okay, now how do i add/ delete records from an existing table? I added the an insert sql statement, but it doesn’t seem to add anything. My code doesn’t say there are any syntax errors though

if(!$connection)  // prints connection error if any
{
  print(mysqli_connect_error());
}

$query = mysqli_query($connection,"SELECT * FROM people");


$sql = "INSERT INTO people (ID, Name)
VALUES ('1', 'Doe')";

if(!$query)  // prints query error if any
{
   mysqli_error($connection);
}
mysqli_close($connection);  // disconnects $connection from MySQL server
?>
</body>
</html>

#10

No, that’s not the way it works.

mysqli_query() is the function which executes the databases queries. Therefore it should be like this:

$query = mysqli_query($connection,"INSERT INTO people (ID, Name) VALUES ('1', 'Doe');");

and get rid of this code. It only assigns the query string to $sql but it doesn’t execute it.

$sql = "INSERT INTO people (ID, Name)
VALUES ('1', 'Doe')";


For better reference please read more on W3Schools.


#11

okay, that part works now! I find myself often confused looking at examples online since everyone has a different way of writing code, so sorry for so many questions.
How would I print out the data into a table on my page now?

if(!$connection)  // prints connection error if any{
  print(mysqli_connect_error());
}

$query = mysqli_query($connection,"SELECT * FROM people");


$sql = "INSERT INTO people (ID, Name, Address)
VALUES ('5', 'new','fsf')";
$query = mysqli_query($connection,$sql . ";");


if(!$query)  // prints query error if any{
   mysqli_error($connection);
}
mysqli_close($connection);  // disconnects $connection from MySQL server
?>

#12

Because mysqli_query() returns data as object, we’ll need to use other functions to convert the data in a ‘friendlier’ way. We could use mysqli_fetch_row() or mysqli_fetch_all() for that.

mysqli_fetch_all() converts the result of mysqli_query() into an array. But please consider reading more about these functions on W3Schools or PHP.NET.

<?php

$host = 'xxx';
$username = 'xxx';
$password = 'xxx';
$database = 'xxx';

$connection = mysqli_connect($host, $username, $password, $database); // connect and select the database at the same time;

if(!$connection)  // prints connection error if any{
  print(mysqli_connect_error());
}

$query = mysqli_query($connection,"SELECT * FROM people");

$rows = mysqli_fetch_all($query); // convert result to array

print_r($rows); // print result

mysqli_close($connection);  // disconnects $connection from MySQL server

?>

#13

Great! everything works as it should!
Is it bad practice to take out the if 2 error statements to make my program a little less clunky?

and on an unrelated note; I tried to assign my domain name from google to my website. Anytime I try to access it from that name, it says “StayTuned”. Should I just wait a few days, or do I have to take some other steps?


#14

Is it bad practice to take out the if 2 error statements to make my program a little less clunky?

Uhm, I added those statements to be easier for you to understand. I suggest you to leave the one related to your database connections. This is the most important one.

and on an unrelated note; I tried to assign my domain name from google to my website. Anytime I try to access it from that name, it says “StayTuned”. Should I just wait a few days, or do I have to take some other steps?

Since this week 000webhost will not allow parking/linking domains from other places except Hostinger. If you want to park/link a domain to your website, you should consider buying it from there.

BUT if you have already parked/linked your domain, this rule does not apply. What is the domain in question?


#15

www.logansgalaxy.com


#16

I don’t know if it will work, but please follow this tutorial to park your domain to 000webhost.