Jump to content

Importing Into Mysql Database


niche

Recommended Posts

I need to import TEST.TXT (located in the same directory as my scriptIt has 1 Record: "6","Lunch","234"Iget this error message: Warning: mysqli_close() expects exactly 1 parameter, 0 given in C:\wamp\www\test\import.php on line 19Why?Heres my script:<?php// Connect to the MySQL server and select the corporate database$mysqli = new mysqli("localhost","user,"password,"test");// Open and parse the test.txt file$fh = fopen("test.txt", "r");while ($line = fgetcsv($fh, 1000, ",")){$id = $line[0];$item = $line[1];$price = $line[2];// Insert the data into the sales table$query = "INSERT INTO example SET id='$id',item='$item', price='$price'";$result = $mysqli->query($query);}fclose($fh);mysqli_close();?>

Link to comment
Share on other sites

$mysqli->close() worked. Thank you very much.But, how am I to get "$mysqli->close()" from the reference you provided?What prior knowledge does the reference assume?

Link to comment
Share on other sites

I guess it assumes you already know PHP, it's not a teaching resource, just a reference of everything that PHP has.The object-oriented syntax is written like this:bool mysqli::close ( void )That means that the mysqli object has a method called close, which takes no parameters (void), and which returns a boolean value (true/false). You created your mysqli object here:$mysqli = new mysqli(...);So, in your code the mysqli object that you're using is called $mysqli. Since the mysqli object has a close method, you access that using $mysqli->close().The other style listed is procedural style, which is defined like this:bool mysqli_close ( mysqli $link )That shows a function called mysqli_close which takes a mysqli object as a parameter and returns a boolean. That's the version you had in your code, the error message was because you weren't sending it a mysqli object as a parameter.

Link to comment
Share on other sites

I've been reading to get my terminology right.In this example $msqli is an object of the class mysqli and close() (ie $mysqli->close():) is a method of the mysqli class.Correct so far?

Link to comment
Share on other sites

Point of information: I found the script I'm using in a book.So, in the line: $result = $mysqli->query($query);, is it accurate to say that query is a method of the $mysqli object, in the mysqli class AND $query satisfies the parameters of the query() method?

Link to comment
Share on other sites

Right, the object-oriented style of mysqli query is like you describe. You could also write that script using procedural syntax instead of object-oriented. PHP was only procedural for a while, they introduced classes in PHP 4 and really became OOP-capable with PHP 5. PHP 4 had classes, but it was still obviously based on a procedural framework. PHP 5 added support for more features that are common to object-oriented languages in general which PHP 4 lacked. Most PHP code you'll see is in the procedural style, but the major reason for that is because most people who have learned PHP have learned off of procedural code examples. You'll need to understand procedural programming in order to use PHP, but if you also understand object-oriented you'll be in a good position to write better code.This is the procedural version of your script above, this will do the same thing, use the same MySQLi extension, but use the other style to do it:

<?php// Connect to the MySQL server and select the corporate database//$mysqli = new mysqli("localhost","user,"password,"test");$conn = mysqli_connect('localhost', 'user', 'password', 'test');// Open and parse the test.txt file$fh = fopen("test.txt", "r");while ($line = fgetcsv($fh, 1000, ",")){  $id = $line[0];  $item = $line[1];  $price = $line[2];  // Insert the data into the sales table  $query = "INSERT INTO example SET id='$id', item='$item', price='$price'";  //$result = $mysqli->query($query);  $result = mysqli_query($conn, $query);}fclose($fh);//$mysql->close();mysqli_close($conn);?>

Link to comment
Share on other sites

More or less. In the table of contents in the manual there, it looks like the methods they mark with :: have both a procedural and object-oriented style, and the methods they mark with -> have only an object-oriented style. The :: operator is a generic class-reference operator, and the -> operator denotes that you're accessing a method on a specific object. In PHP the :: operator is used to specify which namespace you're accessing:http://www.php.net/manual/en/language.oop5...nekudotayim.phpI'm either remembering something from school or just spouting random garbage, but I think in a lot of other OOP languages the :: operator is used to refer to a member of a static class. With a static class, you don't need to create an object to use the class. That's what the first two examples on the link above are doing, they're referring to properties or methods in the class without creating an instance of the class using the new operator. If you know Javascript, a good analogue of a static class in Javascript is the Math object. You don't have to create an instance of the Math object in order to use it, it's always defined. If you want to generate a random number, you just use Math.random. So you could create a static class in PHP where you don't have to create an instance of it, you can just access the properties and methods directly using the :: operator, like they show in the first two examples there. In the third example they use the new operator to create an instance of the class, and then call the method on it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...