Welcome to the Linux Foundation Forum!

How to run php to connect to mysql on debian web server

I having trouble writing the right script to connect my website to a SQL database they are both on the same server I was just wondering if there any suggestion that I could get on the script that I'm trying to run. I took out the password on purpose

<?php<br />
$db_host = "10.0.0.22";

$db_username = "root";

$db_pass = "blank";

$db_name = "test";

@mysql_connect ("$db_host","$db_username","$db_pass") or die ("could not mysql");

@mysql_select_db("$db_name") or die ("no database");

echo"Sussecful Connection";

?>

the problem that I am running into is that I don't get anything telling me if it connected or not

Comments

  • Posts: 6
    edited April 2015
    Hi there,

    Try this :
    1. <?php
    2. $db_host = "10.0.0.22";
    3. $db_username = "root";
    4. $db_pass = "blank";
    5. $db_name = "test";
    6.  
    7. $connect = mysql_connect ($db_host,$db_username,$db_pass);
    8. mysql_select_db($db_name, $connect);
    9.  
    10. //Here you put your SQL query
    11.  
    12. ?>

    You was wrong on the mysql_select_db, also a variable do not need double quote for be processed inside a php fonction.

    Edit : You Should not use it, replace instead by mysqli :
    1. <?php
    2. //Settings mySQL variable
    3.  
    4. $db_host = "127.0.0.1";
    5. $db_name = "dbname";
    6. $db_username = "myuser";
    7. $db_password = "mypassword";
    8.  
    9. //Connect to mySQL
    10.  
    11. $connect = mysqli_connect($db_host, $db_username, $db_password, $db_name);
    12.  
    13. //Set, UTF8 output charset
    14.  
    15. mysqli_set_charset($connect, "utf8");
    16. ?>

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training