Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a program I am currently trying to change from MySQL to MySQLi. The following database configuration is in one file:
<?php
define ("DBHOST","localhost");
define ("DBUSER","user");
define ("DBPASS","pass");
define ("DBNAME","name");
/*define ("DBUSER","root");
define ("DBPASS","");
define ("DBNAME","date_demo");*/
define ("DBPREFIX","");
define ("CHAR_SET","utf8");
define ("CACHEDIR","");
?>
The other section is on another page:
$conn = new mysqli(DBHOST,DBUSER,DBPASS) or die(mysqli_connect_errno());
$row = mysqli_fetch_array(mysqli_query("select * from settings where id = '1' "));
How can I get them all onto one page? Any ideas would be much appreciated so I can start converting to MySQLi.
by doing this, you are borderline mixing procedural and OOP php which is highly ill advised. If you want to keep the connection seperate, you can
include 'conn.php';
however i would highly reccomend looking into building a PDO object and running any queries via that. a useful tutorial can be found at https://www.youtube.com/watch?v=c_hNNAdyfQk&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc which introduces the concepts of using global variables and shows how to instatiate a DB object which creates a better structure for your code and abstracts functions from data.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Ex: If I have code :
$data = array('a','b','c');
foreach($data as $val){
mysql_query("INSERT INTO db (`title`)VALUES('$val')");
}
I want to insert all data from variable $val how can I coding it ?
please help !!!
thank !!
There are two things wrong which i can see at a glance.
mysql is deprecated, use mysqli instead. Reference
Since you will have to switch over to mysqli you will need to reference your database connection every time you want to do a mysql database related operation, unless you do some PHP magic (classes or methods).
Other than these two i guess there is no problem in your code, the for loop should work perfectly fine.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Read a lot of posts about storing sessions inside database but no one seems to provide proper information.Then i came across this post and i found it to be very helpful.
http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/
Now what i don't understand with these posts is that they are taking a class named Database which contain certain functions in order to make session class work i.e. to store sessions inside database.
Question : My question to you all is if it is possible please provide me with that database class even it means creating one own database class file.Just to make sure all the functions that the session class is looking inside database class are found and working.
Codepad:http://codepad.org/mtvT3XXB
That article you cite has horrible issues with wording (example: "...we instantiate a copy of the database class...", which is just plain nonsense). But the basic thing behind such "database" class simply is to keep things generic for the reader, which makes sense.
To interact with a database you need some routines for things like connection handling, query execution and preparation and the like. These routines are typically implemented as methods of a class. Such a class is what the author refers to. He does not name a specific one since they are more or less exchangeable.
You don't actually have to implement your own class, you can use one of those already provided. A short overview is given in the php documentation (which you should read!):
http://php.net/manual/en/set.mysqlinfo.php
I suggest you pick the mysqli connector and go through a few tutorials to learn what it does and how to use it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a simple php connection to my database, I think i did most of the process right, but when I hit the submit button it just renders my actual php file on my screen.
Make sure you are using a web server that supports PHP. It is rendering the code because PHP is not processing your code and renders it as text.
Try using another web server or install PHP on your current setup.
Also use MySQLi or PDO for your database part (it's safer and mysql_* is deprecated).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Sorry for changing the question
I'm trying to include another php file inside php, my code is like this
Ajax mysqli not working
The difference is
I change "$con = new mysqli("localhost", "root", "", "whatever");" with "include 'admin/connection.php';"
Without include it's working, but, after adding include it's keep giving me error :(
Is it just me, my code is wrong or ajax didn't support php include?
if 'admin/connection.php' contains Connection code to DB
use require require PHP instead of include
require will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
require('admin/connection.php');
and if your server is not connected to db how can you expect to run sql query
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've decided to switch to MySQLi, because some people have told me it's more secure.. What I'm really confused about, is the new extensions. I tried just added 'i' after every mysql, but that gave me a crap load of errors. I looked up in the PHP manual why that was happening and there was a whole bunch of other functions.. I honestly can't figure out how to convert. Can you help me out?
include("dbinfo.php");
mysql_connect($c_host,$c_username,$c_password);
#mysql_select_db($c_database) or die(mysql_error());
$mycon = new mysqli($c_host, $c_username, $c_password, $c_database);
$query="SELECT * FROM users WHERE username='" .$_COOKIE['username']. "'";
$result=mysql_query($query) or die(mysql_error());
$num=mysql_numrows($result);
$username=mysql_result($result,$i,"username");
Here's what you need to do:
Read the overview so that have an understanding of the differences/advantages.
Consult the old -> new function summary on the PHP site and get your existing code up and running with the mysqli interface.
Take advantage of the improvements (such as using prepared statements) otherwise this is a futile exercise. (By default mysqli really isn't any more secure than mysql.)
One of the reasons MySQLi is more "secure" is because it offers a different interface, which is better in many ways. Instead of trying to translate your code directly, learn the new interface and use it. If that's all your code, it wouldn't be easy to rewrite from scratch, and which is more important, look up the equivalents (and alternatives) for everything you're doing in the code that you pasted.
For starters, you should use $mysqli->prepare with parameters instead of interpolating variables like you're doing.
http://www.php.net/manual/en/mysqli.prepare.php