Insert data to database using foreach loop php [closed] - php

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.

Related

MySql (XAMPP) sometimes has no data in table [closed]

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 2 years ago.
Improve this question
I use MYSQL, PHP 7.2 and MVC architecture but sometimes after inserting data into invoice_table table, invoice_table shown as an empty table, after couple minutes data will come back again.
My Problem is solved by RESTARTING MySql service and thanks to #ADyson and #Your_Common_Sense for Helping me :|.
real Thanks to #Undry.

What is the purpose of using load(array) when checking to see if something exists? [closed]

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 6 years ago.
Improve this question
I am attempting to debug a php script I did not write. In order to check if a location already exists, they used the following:
$locTable = JTable::getInstance('Location', 'DPCalendarTable');
if ($locTable->load(array('latitude' => $data['latitude'],'longitude' => $data['longitude'])){
$_eventLocCache[$data['alias']] = (int)$locTable->id;
}
What is the purpose of "..->load(array..."?
Since you use Joomla it retrieves data from the table called Location
JTable::load() - Loads a row from the database and binds the fields
to the object properties.

Error encounter using PHP and MySQL [closed]

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 6 years ago.
Improve this question
I have problem about PHP and MySQL. I write information to MySQL database, but "ü,ö,ğ,ç,ş," letters change to çşöğüıə.
Looks like an encoding problem.
Try utf8_encode() function.
You may wanna refer to this: utf8_encode() manual
Hope this helps.

how can i put the database and connection on same file [closed]

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.

How to implement a loop in php [closed]

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
Hi I'm totally new here and with and would glad if someone could help as I'm stuck with how I can create a loop with some php code.
$TotalUniques = $this->TotalUniquesHits($Counts[$xx]['ID']);
The value I'm after is [$xx] which represents a position in the array. How to change it so that I get $TotalUniques looping through all the values of $xx?
You can try with foreach loop:
foreach($Counts as $xx){
$TotalUniques[] = $this->TotalUniquesHits($xx['ID']);
}
You may also consider checking if each $xx has ID key with isset or array_key_exists.

Categories