1

I have a problem with my code. I'm trying to add new post to the table events. I'm confused because I have used this code in other place on the same website (but it was using mysqli_query to register new user). mysqql_error returns "No database selected"

This is the code:

<?php
$add_title = $_POST['add_title'];
$add_happen = $_POST['add_happen'];
$add_created = date('Y-m-d');
$add_content = $_POST['add_content'];
$add_author = $_POST['add_author'];

//connect to 
    //localhost
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "zhp2";
$db_con = mysql_connect($db_host, $db_username, $db_password, $db_dbname); 

$query = "
    INSERT INTO events ( title, happen, created, content, author ) 
    VALUES ( '$add_title', '$add_happen', '$add_created', '$add_content', '$add_author') )
";

$retval = mysql_query($query,  $db_con);
if(! $retval ){
    die('Could not enter data: ' . mysql_error());
}
else{
    echo "Entered data successfully\n";
}

mysql_close($db_con);

//header('Location: ../../index.php?link=events');?>

I've tried to fix it using trial and error method playing with different combinations both mysql_query and mysqli_query

2

3 Answers 3

4

You are confusing mysql_connect and mysqli_connect functions in the way you pass those parameters. In your example:

$db_con = mysql_connect($db_host, $db_username, $db_password, $db_dbname);  

you are passing a fourth parameter which is the database name but that wont work as you should only pass the three first (host,username,password) and then call mysql_select_db():

$db_con = mysql_connect($db_host, $db_username, $db_password);   
mysql_select_db( $db_dbname, $db_con );

In mysqli which is the BETTER way of doing it since mysql_ functions are very vulnerable and being deprecated from php you could pass four elements like here:

$db_con = mysqli_connect($db_host,$db_username, $db_password, $db_dbname) or die("Error " . mysqli_error($link)); 

which is close to what you are trying to do, but in a correct mysqli_ way.

Sign up to request clarification or add additional context in comments.

Comments

4

Well then, you need to select the database! ;) The fourth parameter of mysql_connect() is not the database name. You need to do this separate of connecting to the MySQL server.

Using mysql_select_db() function:

$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "zhp2";
$db_con = mysql_connect($db_host, $db_username, $db_password );
mysql_select_db( $db_dbname, $db_con );

And of course all the obligatory warnings about SQL injection, sanitizing your data, deprecation of mysql_* functions.

Comments

0

You need to select which database to connect to using the mysql_select_db function:

// make $db_dbname the current db
$db_selected = mysql_select_db($db_dbname, $db_con);
if (!$db_selected) {
    die ("Can't use $db_dbname : " . mysql_error());
}

See the PHP manual for more info: http://php.net/manual/en/function.mysql-select-db.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.